package com.aivin.lib_login.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.aivin.lib_base.spiders.IGetOneShareSpiderCallBack;
import com.aivin.lib_base.spiders.SpiderOneShareDataTool;
import com.aivin.lib_base.tool.MyDeviceInforHelper;
import com.aivin.lib_login.R;
import com.aivin.lib_login.R2;
import com.aivin.lib_login.adapter.PicAdapter;
import com.aivin.lib_login.bean.PicBean;
import com.aivin.lib_login.icallback.StaggeredGridLayoutManagerEndlessRecyclerOnScrollListener;
import com.aivin.lib_login.tool.ListPageUtil;
import com.aivin.lib_login.ui.RecyclerViewFastScroller;
import com.aivin.lib_widget.base.BaseActivity;
import com.aivin.lib_widget.bean.MarginDecorationForStaggered;
import com.alibaba.fastjson.JSONObject;
import com.imglib.ImagePreviewManager;
import com.imglib.bean.LoadStrategy;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;


/**
 *  网络图片，缩略图查看
 */
public class PicShowActivity extends BaseActivity {
    @BindView(R2.id.recyclerviewPics)
    RecyclerView recyclerView;
    private Context context ;
    private SpiderOneShareDataTool spiderOneShareDataTool = new SpiderOneShareDataTool();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.lib_media_activity_picshow);
        ButterKnife.bind(this);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        context = this ;

        initView();
        getPics();
    }

    private void  getPics(){
        spiderOneShareDataTool.getOneShareData("https://gitee.com/hnyer/workpics/raw/master/%E8%BD%BB%E6%9D%BE%E4%B8%80%E5%88%BB1_json.md" ,iGetOneShareSpiderCallBack ,2);
    }

    private ListPageUtil listPageUtil ;
    private IGetOneShareSpiderCallBack iGetOneShareSpiderCallBack =
            new IGetOneShareSpiderCallBack() {
                @Override
                public void onGetData(String jsonStr ,int type) {
                    hideLoadDialog2();
                    List<String> picPaths = JSONObject.parseArray(jsonStr, String.class);
                    listPageUtil = new ListPageUtil(picPaths, 30);  // 每次加载30条


                    recyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            boolean hasNex =listPageUtil.hasNext() ;
                            if(!hasNex){
                                // 如果已经加载完成
                                adapter.setHasMoreData(false);
                                return;
                            }
                            List<PicBean> pageData =  getNewData () ;// listPageUtil.nextPage();
                            adapter.updateData(pageData); //  init posst
                            adapter.setHasMoreData(true);
                        }
                    });
                }

                @Override
                public void onStartLoad() {
                    showLoadDialog2(context);
                }

                @Override
                public void onError(String error) {
                    hideLoadDialog2();
                }
            };



    private PicAdapter adapter ;
    private void initView(){
        recyclerView.setHasFixedSize(true);
        // 瀑布流分3列显示
        int columnCount = 3;

        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(columnCount,StaggeredGridLayoutManager.VERTICAL){
            @Override
            public void onLayoutCompleted(RecyclerView.State state) {
                super.onLayoutCompleted(state);
                //WkLog.showLog("onLayoutCompleted....");
            }
        } ;

//        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);

        final RecyclerViewFastScroller fastScroller = findViewById(R.id.fastscroller);
        fastScroller.setRecyclerView(recyclerView);
        fastScroller.setViewsToUse(R.layout.recycler_view_fast_scroller__fast_scroller, R.id.fastscroller_bubble, R.id.fastscroller_handle);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new MarginDecorationForStaggered(context ,columnCount ,MyDeviceInforHelper.dp2px(context ,2)));
        recyclerView.addOnScrollListener(new StaggeredGridLayoutManagerEndlessRecyclerOnScrollListener() {
            @Override
            public void onLoadMore() {
                if(listPageUtil ==null){
                    return;
                }

                boolean hasNex =listPageUtil.hasNext() ;
                if(!hasNex){
                    // 如果已经加载完成
                    adapter.setHasMoreData(false);
                    return;
                }
                List<PicBean> pageData =  getNewData () ;
                // todo 临时关闭 调试。。
                /*adapter.updateData(pageData); // onLoadMore
                adapter.setHasMoreData(true);*/
            }
        });

        List<PicBean> datas = new ArrayList<>() ;
        adapter = new PicAdapter(  datas, columnCount ,new PicAdapter.PicSelectCallBack() {
            @Override
            public void setSelectedCallBack(int index) {
                //openPicShowActivity(index ,adapter.getDatas());
            }
        });
        recyclerView.setAdapter(adapter);
    }



    private List<PicBean> getNewData(){
        List<PicBean> pageData = new ArrayList<>();
        List<String> temp = listPageUtil.nextPage();
        for(String str: temp){
            pageData.add(new PicBean(str));
        }
        return pageData ;
    }

    public void openPicShowActivity(  int index , List<String> picPaths) {
        ImagePreviewManager.getInstance()
                .setContext(context )
                .setIndex(index)
                .setImageList(picPaths)  // 图片集合
                .setLoadStrategy(LoadStrategy.AlwaysThumb)
                .setFolderName("BigImageView")
                .setZoomTransitionDuration(300)
                .setShowErrorToast(true)
                .setEnableDragClose(true)
                .setEnableUpDragClose(true)
                .setEnableDragCloseIgnoreScale(false)
                .setShowCloseButton(true)
                .setShowDownButton(true)
                .setDownIconResId(R.mipmap.icon_download_new)
                .setShowIndicator(true)
                .setIndicatorShapeResId(R.drawable.shape_indicator_bg)
                .setErrorPlaceHolder(R.mipmap.load_failed)
                .start();
    }




}